package com.ly.user_system.code;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
/**
 * <p>
 * 系统配置表 前端控制器
 * </p>
 *
 * @author lyh
 * @since 2025-05-10  11:00:47
 */
@RestController
@RequestMapping("/code")
public class CodeController {


    @PostMapping
    public void renderTemplate(@RequestBody CodeDto codeDto){
        String entity =StrUtil.upperFirst(StrUtil.toCamelCase(codeDto.getEntity()));
        List<Column> columns = codeDto.getColumns();
        columns.forEach(item-> item.setDbName(StrUtil.toUnderlineCase(item.getFieldName())));
        String packagePath=ReUtil.replaceAll(this.getClass().getPackageName(),"\\.[^.]+$", "");
        HashMap<String,Object> context=new HashMap<>();
        context.put("tableName",codeDto.getEntity());
        context.put("packagePath", packagePath);
        context.put("Entity", entity);                  
        context.put("Detail", entity+"Detail");             
        context.put("DTO", entity+"DTO");              
        context.put("PQuery", entity+"Query");            
        context.put("M", entity+"Mapper");               
        context.put("Service", entity+"Service");
        context.put("columns", columns);
        context.put("baseEntity", "BaseEntity");
        context.put("baseEntityPackage", "com.ly.user_system.code");
        context.put("baseDto", "BaseDto");

        String serviceStr = CodeGenerate.renderTemplate(context, "templates/Service.vm");
        String serviceImplStr = CodeGenerate.renderTemplate(context, "templates/ServiceImpl.vm");
        String controllerStr = CodeGenerate.renderTemplate(context, "templates/Controller.vm");
        String dtoStr = CodeGenerate.renderTemplate(context, "templates/DTO.vm");
        String entityStr = CodeGenerate.renderTemplate(context, "templates/Entity.vm");
        String mapperStr = CodeGenerate.renderTemplate(context, "templates/Mapper.vm");
        String mapperXmlStr = CodeGenerate.renderTemplate(context, "templates/XmlMapper.vm");
        String detailStr = CodeGenerate.renderTemplate(context, "templates/Detail.vm");
        String queryStr = CodeGenerate.renderTemplate(context, "templates/PQuery.vm");

        try{
            String basePath= Paths.get(System.getProperty("user.dir"), "src\\main\\java\\"+StrUtil.replaceChars(packagePath,".","\\")) +"\\";
            String xmlPath= Paths.get(System.getProperty("user.dir"), "src\\main\\resources\\mapper")+"\\";
            FileUtil.appendUtf8String(entityStr,new File(basePath+"entity\\"+entity+".java"));
            FileUtil.appendUtf8String(serviceImplStr,new File(basePath+"service\\impl\\"+entity+"ServiceImpl"+".java"));
            FileUtil.appendUtf8String(controllerStr,new File(basePath+"controller\\"+entity+"Controller"+".java"));
            FileUtil.appendUtf8String(dtoStr,new File(basePath+"dto\\"+entity+"DTO.java"));
            FileUtil.appendUtf8String(serviceStr,new File(basePath+"service\\"+entity+"Service"+".java"));
            FileUtil.appendUtf8String(mapperStr,new File(basePath+"mapper\\"+entity+"Mapper"+".java"));
            FileUtil.appendUtf8String(detailStr,new File(basePath+"detail\\"+entity+"Detail"+".java"));
            FileUtil.appendUtf8String(queryStr,new File(basePath+"req\\"+entity+"Query"+".java"));
            FileUtil.appendUtf8String(mapperXmlStr,new File(xmlPath+entity+"Mapper"+".xml"));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }





}
